{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e0c30cda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import os\n",
    "import fitz  # fitz就是pip install PyMuPDF\n",
    " \n",
    "def pyMuPDF_fitz(pdfPath, imagePath):\n",
    "    startTime_pdf2img = datetime.datetime.now()  # 开始时间\n",
    "    print(\"imagePath=\" + imagePath)\n",
    "    pdfDoc = fitz.open(pdfPath)\n",
    "    for pg in range(pdfDoc.page_count):\n",
    "        page = pdfDoc[pg]\n",
    "        rotate = int(0)\n",
    "        # 每个尺寸的缩放系数为1.3，这将为我们生成分辨率提高2.6的图像。\n",
    "        # 此处若是不做设置，默认图片大小为：792X612, dpi=96\n",
    "        zoom_x = 2  # (1.33333333-->1056x816)   (2-->1584x1224)\n",
    "        zoom_y = 2\n",
    "        mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)\n",
    "        pix = page.get_pixmap(matrix=mat, alpha=False)\n",
    "        if not os.path.exists(imagePath):  # 判断存放图片的文件夹是否存在\n",
    "            os.makedirs(imagePath)  # 若图片文件夹不存在就创建\n",
    "        pix.pil_save(imagePath + '/' + 'images_%s.png' % pg)  # 将图片写入指定的文件夹内\n",
    "    endTime_pdf2img = datetime.datetime.now()  # 结束时间\n",
    "    print('pdf2img时间=', (endTime_pdf2img - startTime_pdf2img).seconds)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "89534a39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "imagePath=./imgs\n",
      "pdf2img时间= 39\n"
     ]
    }
   ],
   "source": [
    "pdfPath = '检验检疫风险管理研究 孙远志，吴文忠.pdf'\n",
    "# 2、需要储存图片的目录\n",
    "imagePath = './imgs'\n",
    "pyMuPDF_fitz(pdfPath, imagePath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe0fac88",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
